import 'dart:math';
import 'package:dart_date/dart_date.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'dismiss_keyboard_widget.dart';

class ConsoleController extends GetxController {
  static final ConsoleController instance = ConsoleController._();

  ConsoleController._();

  var consoleList = <String>[].obs;
  var inputModel = 0.obs;
  final maxLength = 100;
  var expanded = false.obs;
  late var top = (Get.height / 5).obs;
  var right = 0.0.obs;
  final scrollController = ScrollController();
  final textController = TextEditingController();

  void addConsole(String str) {
    final time = DateTime.now();
    final timeStr = time.format("H:m:s.") + time.millisecond.toString();
    if (consoleList.length > maxLength - 1) {
      consoleList.removeRange(0, consoleList.length - (maxLength - 1));
    }
    consoleList.add("$timeStr:   $str");
  }

  void updatePosition(DragUpdateDetails detail) {
    top.value = min(max(0, top.value + detail.delta.dy), Get.height - 38);
    right.value = min(max(right.value - detail.delta.dx, 0), Get.width - 38);
  }

  void clearConsole() {
    consoleList.clear();
  }
}

class ConsoleWidget extends StatelessWidget {
  const ConsoleWidget({super.key});
  
  @override
  Widget build(BuildContext context) {
    return GetBuilder(
      init: ConsoleController.instance,
      builder: (controller) {
        return Obx((){
            return Positioned(
                  top: controller.top.value,
                  right: controller.right.value,
                  child: Material(
                    color: Colors.transparent,
                    child: GestureDetector(
                      onPanUpdate: (details) {
                        controller.updatePosition(details);
                      },
                      child: DismissKeyboardWidget(
                        child: Stack(
                          children: [
                            if (controller.expanded.value)
                              Container(
                                height: Get.height * 0.75,
                                width: Get.width * 0.75,
                                color: Colors.green,
                                child: Stack(
                                  children: [
                                    buildConsoleList(),
                                    Positioned(
                                      left: 0,
                                      right: 0,
                                      top: 0,
                                      height: 30,
                                      child: Row(
                                        children: [
                                          GestureDetector(
                                            onTap: () {
                                              controller.expanded.value = false;
                                            },
                                            child: Icon(Icons.close, size: 24),
                                          ),
                                          Expanded(child: Container()),
                                          GestureDetector(
                                            onTap: () {
                                              controller.clearConsole();
                                            },
                                            child: Icon(Icons.delete, size: 20),
                                          )
                                        ],
                                      ),
                                    ),
                                  ],
                                ),
                              ),
                            if (!controller.expanded.value)
                              GestureDetector(
                                onTap: () {
                                  controller.expanded.value =
                                      !controller.expanded.value;
                                },
                                child: Container(
                                  decoration: BoxDecoration(
                                      borderRadius: BorderRadius.circular(99),
                                      color: Colors.red),
                                  height: 38,
                                  width: 38,
                                  child: const Center(
                                    child: Text(
                                      "打印台",
                                      style: TextStyle(
                                          fontSize: 12,
                                          fontWeight: FontWeight.normal,
                                          color: Colors.black),
                                    ),
                                  ),
                                ),
                              ),
                          ],
                        ),
                      ),
                    ),
                  ),
                );
          }
        );
      }
    );
  }

  Widget buildConsoleList() {
    return GetBuilder<ConsoleController>(
      builder: (controller) {
      return Padding(
        padding: const EdgeInsets.only(left: 8, right: 8, bottom: 8, top: 32),
        child: ListView.separated(
          controller: controller.scrollController,
          padding: EdgeInsets.zero,
          itemBuilder: (context, index) {
            return Text(
              controller.consoleList[index],
              style: const TextStyle(
                  decoration: TextDecoration.none,
                  fontSize: 12,
                  fontWeight: FontWeight.normal,
                  color: Colors.black),
            );
          },
          separatorBuilder: (context, index) {
            return const Divider();
          },
          itemCount: controller.consoleList.length,
        ),
      );
    });
  }
}
